/*
@author: stanfor
@date: 2022/9/16 18:33
@Version: 1.0
@last modify time : 2022/9/16 18:33
*/

import moment from 'moment';
import type { Moment } from 'moment';
import type { PickerLocale } from 'antd/lib/date-picker/generatePicker';

export type PickerPanel<DateType> = {
  // eslint-disable-next-line no-unused-vars
  disabledDate: (date: DateType) => boolean;
  disabledType: 'date' | 'month' | 'quarter' | 'week' | 'weeks' | 'weekday' | 'year';
}

/** DisabledOption
 * @author: JasonStandFor
 * @description Desc:: 日期禁用可选项, 改造后支持原有方法传参且支持禁用数组选项
 * @example
 例1:
 disabledDate={[1, 2, 5]} // 数组里最多存放 7 个禁用日期
 例2:
 disabledDate={(date) => {
    // console.log('date: ', date.date());
    // 1、15 可选，其它禁用
    return date.date() === 15 || date.date() === 1
    // 15-31 可选，小于 15 禁用
    // return date.date() < 15
  }}
 **/
export declare type DisabledOption = PickerPanel<Moment>['disabledDate']|Array<number>;

/** DisabledOption
 * @author: JasonStandFor
 * @description Desc:: 日期禁用可选项, 改造后支持原有方法传参且支持禁用数组选项
 * @param {DisabledOption} disabledDate - 禁用日期可选项
 * @param {String} disabledType - 禁用日期的类型
 **/
export const DisabledDateFn = (disabledDate?: DisabledOption, disabledType: PickerPanel<string>['disabledType'] = 'date') => {
  let updateDisabledDateFn: any = null;
  if (Array.isArray(disabledDate)) {
    updateDisabledDateFn = (M: Moment = moment()) => {
      try {
        if (disabledDate.length > 7) {
          throw new Error('unexpected: 传入的 disabledDate.length 超出了预期长度7');
        }
        switch (disabledDate.length) {
          case 1:
            return M[disabledType]() === disabledDate[0];
          case 2:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1];
          case 3:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1] ||
              M[disabledType]() === disabledDate[2];
          case 4:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1] ||
              M[disabledType]() === disabledDate[2] || M[disabledType]() === disabledDate[3];
          case 5:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1] ||
              M[disabledType]() === disabledDate[2] || M[disabledType]() === disabledDate[3] ||
              M[disabledType]() === disabledDate[4];
          case 6:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1] ||
              M[disabledType]() === disabledDate[2] || M[disabledType]() === disabledDate[3] ||
              M[disabledType]() === disabledDate[4] || M[disabledType]() === disabledDate[5];
          case 7:
            return M[disabledType]() === disabledDate[0] || M[disabledType]() === disabledDate[1] ||
              M[disabledType]() === disabledDate[2] || M[disabledType]() === disabledDate[3] || M[disabledType]() === disabledDate[4] ||
              M[disabledType]() === disabledDate[5] || M[disabledType]() === disabledDate[6];
          default:
            return false;
        }
      } catch (e) {
        console.error(e);
      }
      return false;
    }
  } else {
    updateDisabledDateFn = (date: Moment) => {
      try {
        if (disabledDate) {
          return disabledDate(date);
        }
      } catch (e) {
        console.error(e);
      }
      return false;
    }
  }
  return updateDisabledDateFn
};

export const LocaleDatePicker: PickerLocale = {
  lang: {
    'locale': 'zh-CN',
    'placeholder': '选择日期',
    'rangePlaceholder': ['开始时间', '结束时间'],
    'today': '今天',
    'now': '现在',
    'backToToday': '回到今天',
    'ok': 'OK',
    'clear': '清除',
    'month': 'Month',
    'year': 'Year',
    'timeSelect': 'Select time',
    'dateSelect': 'Select date',
    'monthSelect': 'Choose a month',
    'yearSelect': 'Choose a year',
    'decadeSelect': 'Choose a decade',
    'yearFormat': 'YYYY',
    'dateFormat': 'M/D/YYYY',
    'dayFormat': 'D',
    'dateTimeFormat': 'M/D/YYYY HH:mm:ss',
    'monthFormat': 'MMMM',
    'monthBeforeYear': true,
    'previousMonth': 'Previous month (PageUp)',
    'nextMonth': 'Next month (PageDown)',
    'previousYear': 'Last year (Control + left)',
    'nextYear': 'Next year (Control + right)',
    'previousDecade': 'Last decade',
    'nextDecade': 'Next decade',
    'previousCentury': 'Last century',
    'nextCentury': 'Next century',
    // 跟 locale 字段有关 为 zh-CN 配置会失效, 为 en-US 可以正常配置
    // shortWeekDays: ['日', '一', '二', '三', '四', '五', '六'],
    // ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
    // TODO 配置未生效？
    // shortMonths: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
  },
  timePickerLocale: {
    placeholder: '选择时间'
  },
  dateFormat: 'YYYY-MM-DD',
  dateTimeFormat: 'YYYY-MM-DD HH:mm:ss',
  weekFormat: 'YYYY-wo',
  monthFormat: 'YYYY-MM'
};
